Once upon a time I hinted that there were operators that had more than two operands. This is true, but there is only one case of it in C++. This is a ternary operator, the ternary operator. I have found several different technical explanations of this. Some say it is the ternary operator while others say it is a condition-expression using a ternary operator. They are basically both right, but there is only the one ring, er … ternary operator. J (Sorry, been reading Lord of the Rings).
So what is this damn thing I’m talking about? This particular operation, with the ternary operator, uses the question mark (‘?’). It is basically a compressed ‘if’/’else’ that represents a resulting value. And its practically impossible to explain, let me show you an example:
01 |
#include <iostream.h> |
02 |
|
03 |
main() |
04 |
{ |
05 |
int num; |
06 |
cout <<
“Enter a number (1-10):”; |
07 |
cin >>
num; |
08 |
num = (num < 1)?1:num; |
09 |
} |
Blargo.